unit scene;

interface

  uses
    Microsoft.DirectX,
    Microsoft.DirectX.Direct3D,
    System.Drawing,
    Engine;




  type

  CBoule = class
    private
      m_diametre, m_masse : integer;
      m_mesh : Mesh;

    public
      x, y, z : integer;
      constructor Create(radius : integer ; device : Device);
      function getmasse : integer;
      procedure move(u, v, w : integer ; device : Device);
      procedure affiche;
  end;

 CScene = class
    private
      m_hdlEngine : CEngine;
      m_boule : CBoule;
      m_obstacle, m_terrain{, m_boule} : Mesh;
      x : integer;
      m_myVB : VertexBuffer;
      
    public
      constructor Create(engine : CEngine );
      procedure BuildScene;
      procedure Render;
  end;



implementation

  constructor CBoule.Create(radius : integer ; device : Device);
  begin
    inherited Create;
    m_masse := 10;
    m_diametre := radius;
    m_mesh.Sphere(device, m_diametre, 15, 15);
  end;

  function CBoule.getmasse : integer;
  begin
    result := m_masse;
  end;

  procedure CBoule.move(u, v, w : integer ; device : Device);
  begin
    device.Transform.World := Matrix.Translation(u, v, w);
    x := u;
    y := v;
    z := w;
  end;

  procedure CBoule.affiche;
  begin
    m_mesh.drawsubset(0);
  end;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

  constructor CScene.Create(engine : CEngine );
  begin
    inherited Create;
    m_hdlEngine := engine;
    m_boule := CBoule.Create(5, m_hdlEngine.getDevice);
    x := 0;
    BuildScene;
  end;

  procedure CScene.BuildScene;
  var
    verts : array[0..2] of CustomVertex.TransformedColored;
  begin
//  // TODO
//  // Modifier les coordonnes pour faire un sol
//  //
    verts[0] := CustomVertex.transformedColored.Create( 0, 0, 0, 1, Color.Orange.ToArgb );
    verts[1] := CustomVertex.transformedColored.Create( 50, 0, 0, 1, Color.Red.ToArgb );
    verts[2] := CustomVertex.transformedColored.Create( 0, 50, 0, 1, Color.Yellow.ToArgb );

    m_myVB := VertexBuffer.Create( typeof(CustomVertex.transformedColored), 3, m_hdlEngine.getDevice, Usage.None, CustomVertex.Transformedcolored.Format, Pool.Default );
    m_myVB.SetData(verts, 0, LockFlags.None);

  end;

  procedure CScene.Render;
  var
    materiau : material;
  begin
    m_hdlEngine.StartRender;
    m_hdlEngine.getDevice.RenderState.Lighting := true;
    m_hdlEngine.getDevice.RenderState.CullMode := Cull.None;
    m_hdlEngine.getDevice.RenderState.FillMode := Fillmode.Solid;
    m_hdlEngine.getDevice.VertexFormat := CustomVertex.TransformedColored.Format;
    m_hdlEngine.getDevice.SetStreamSource(0, m_myVB, 0);
    m_hdlEngine.getDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1 );

    //materiau.Diffuse := color.Red;
//
//     Tentative pour l'instant loupee d'afficher un sol : un mesh parallelepipedique noir
//     Il vaudrait mieux le remlacer par un rectangle fait de 4 vertex
    materiau.Emissive := color.black;
    m_hdlEngine.getDevice.Material := materiau;
    m_terrain := mesh.Box(m_hdlEngine.getDevice,10, 0.01, 20);
    m_terrain.DrawSubset(0);


    // Affichage du cube Obstacle, ne sert qu'a tester la gestion des collisions.
    m_hdlEngine.getDevice.Transform.world := Matrix.Translation(-5, 1, 0);
    materiau.Emissive := color.red;
    m_hdlEngine.getDevice.Material := materiau;
    m_obstacle := mesh.Box(m_hdlEngine.getDevice, 2, 2, 2);
    m_obstacle.DrawSubset(0);
    //m_hdlEngine.getDevice.Transform.World := matrix.Identity;

    // Affichage du cube vert, cube qui deviendra une boule dans les versions ulterieures
    m_hdlEngine.getDevice.Transform.world := Matrix.Translation(8, 0, 0);
    //m_hdlEngine.getDevice.Transform.World := Matrix.RotationY((System.Environment.TickCount / 400 ) / math.pi);
    materiau.Emissive := color.green;
    m_hdlEngine.getDevice.Material := materiau;
    m_boule.affiche;
//    m_boule := mesh.Sphere(m_hdlEngine.getDevice, 2, 10, 10);
//    m_boule.drawsubset(0);
    x := x + 1;
    //m_hdlEngine.SetupCamera(vector3.Create(6, 4, -x));
    //writeln(x);
    m_hdlEngine.FinishRender;
  end;
end.
